package com.example.demo.service;

import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class DevicePropertyService {

    public String setDeviceProperty(String relay1Value, String relay2Value) {
        // API接口地址
        String url = "http://47.113.106.7:8089/device/service/property/set";

        // 创建RestTemplate实例
        RestTemplate restTemplate = new RestTemplate();

        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("token", "0986bb2e-c72a-4d5e-805f-da96c94b7468");

        // 创建请求体
        String requestBody = String.format("{\n" +
                "  \"data\": {\n" +
                "    \"args\": {\n" +
                "      \"Relay2\": \"%s\",\n" +
                "      \"Relay1\": \"%s\"\n" +
                "    },\n" +
                "    \"deviceId\": \"17405531283010863218076226319015d\"\n" +
                "  },\n" +
                "  \"requestId\": \"debug-test-123\"\n" +
                "}", relay2Value, relay1Value);

        // 创建请求实体
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);

        // 执行请求
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                requestEntity,
                String.class
        );

        // 返回响应体
        return response.getBody();
    }
}